package com.imooc.springmvc3.web;

import com.imooc.springmvc3.domain.DemoObj;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 注解演示控制器
 * Created by joe on 16-8-12.
 */
@Controller
@RequestMapping("/anno")
public class DemoAnnoController {
    /**
     * 此方法未标注路径,因此使用类级别的路径/anno;produces可指定返回的response媒体类型和字符集,
     * 或需要返回值是json对象,则设置produces="application/json;charset=UTF-8"
     * @param request
     * @return
     */
    @RequestMapping(produces = "text/plain;charset=UTF-8")
    public @ResponseBody String index(HttpServletRequest request){
        return "url: " + request.getRequestURL() + "   can access";
    }

    @RequestMapping(value = "/pathvar/{str}",produces = "text/plain;charset=UTF-8")
    public @ResponseBody String demoPathVar(@PathVariable String str, HttpServletRequest request){
        return "url: " + request.getRequestURL() + "  can access,str: " + str;
    }

    @RequestMapping(value = "/requestParam",produces = "text/plain;charset=UTF-8")
    public @ResponseBody String passRequstParam(Long id, HttpServletRequest request){
        return "url: " + request.getRequestURL() + "  can access,id: " + id;
    }

    @RequestMapping(value = "/obj",produces = "application/json;charset=UTF-8")
    public @ResponseBody String passObj(DemoObj obj, HttpServletRequest request){
        return "url: " + request.getRequestURL() + "  can access,obj id: " + obj.getId()+" obj name "+obj.getName();
    }

    @RequestMapping(value = {"/name1","/name2"},produces = "text/plain;charset=UTF-8")
    public @ResponseBody String remove(Long id, HttpServletRequest request){
        return "url: " + request.getRequestURL() + "  can access";
    }
}
